package br.com.aggjpsv.libraryserver.entities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

/**
 * Realiza o parse da requisição recebida
 *
 * @author Thiago Galbiatti Vespa - <a href="mailto:thiago@thiagovespa.com.br">thiago@thiagovespa.com.br</a>
 * @version 0.2
 */
public class Request {

    private HTTPHead head;
    private boolean isData;
    private DataPiles data;

    public Request() {
        head = new HTTPHead();
    }

    /**
     * Realiza o parse
     *
     * @param input
     * @return
     * @throws IOException
     */
    public void parse(String input) throws IOException {
        BufferedReader br = new BufferedReader(new StringReader(input));
        String line = null;
        int lineNumber = 0;
        
        String host = "Host:";
        String referer = "Referer:";
        String contentType = "Content-Type:";
        String contentLength = "Content-Length:";
        
        String data = null;
        while ((line = br.readLine()) != null) {
            //System.out.println(lineNumber + " " + line);
            if (lineNumber == 0) {
                String[] values = line.split(" ");
                if (values.length == 3) {
                    this.head.setMethod(values[0]);
                    this.head.setUri(values[1]);
                    this.head.setProtocol(values[2]);
                }// TODO Tratar erro
            } else {
                String[] values = line.split(" ");
                if (values[0].equalsIgnoreCase(host)) {
                    this.head.setHost(values[1]);
                }
                if (head.getMethod().equalsIgnoreCase("POST")) {
                    if (values[0].equalsIgnoreCase(referer)) {
                        this.head.setReferer(values[1]);
                    }
                    if (values[0].equalsIgnoreCase(contentType)) {
                        this.head.setContentType(values[1]);
                        if (values[1].indexOf("multipart/form-data") != -1) {
                            head.setBoundary(values[2]);
                        }
                    } else {
                        if (values[0].equalsIgnoreCase(contentLength)) {
                            this.head.setContentLength(Integer.parseInt(values[1]));
                            isData = true;
                        } else {
                            if (isData && (head.getContentType().equalsIgnoreCase("application/x-www-form-urlencoded") || head.getContentType().equalsIgnoreCase("text/plain"))) {
                                data = data + line;
                            } else {
                                data = null;
                            }
                        }
                    }
                }
            }   
            lineNumber++;
        }
        if (data != null) {
            this.data.setPilesAction(data);
        }
    }

    public String toString() {
        return head.toString()
                + "data: " + data + "\n";
    }
    
    public boolean isData() {
        return isData;
    }
    
    public DataPiles getData() {
        return data;
    }

    public HTTPHead getHead() {
        return head;
    }
    
}
